%macro get_f1065_k1;

	
	proc printto log = "&logdir/get_f1065_k1.txt" new;
	run;
	

		/****************************
		*	Get the tins 			*
		*	into 'tinlist'			*
		****************************/
		%make_tinlist;

		/****************************************
		*										*
		*			Start PROC SQL				*
		*										*
		****************************************/
		proc sql noprint;
		[connection details redacted]
			create table sample as
			select * from connection to iq 
			(
			select
				b.[redacted] as f1065_div,
				b.[redacted] as f1065_gp,
				b.[redacted] as f1065_int,
				b.[redacted] as f1065_st,
				b.[redacted] as f1065_lt,
				coalesce(b.[redacted],0) + coalesce(b.[redacted],0) + coalesce(b.[redacted],0) -
					coalesce(b.[redacted],0) as f1065_ord,
				a.person_id,
				b.[redacted] as payer_id,
				b.[redacted] as formdate,
				b.[redacted] as f1065_royal
			from #tinlist as a
				inner join [redacted] as b
					on(a.person_id = b.[redacted])
			where b.[redacted] = &base_yr
				and b.[redacted] <= 4
			order by a.person_id,
				payer_id,
				formdate
			);
			disconnect from iq;
			quit;

		data sample;
			set sample;
			by person_id payer_id;
			if last.payer_id;
			

		run;

		proc means data = sample nway noprint;
			class person_id;
			output out = rlib.f1065_k1(drop=_type_ _freq_)
				sum(f1065_div f1065_gp	f1065_int f1065_st f1065_lt f1065_ord f1065_royal) =
					f1065_div f1065_gp	f1065_int f1065_st f1065_lt f1065_ord f1065_royal;
		run;
		

	proc printto;
	run;
%mend;
